package com.sx.manage.service.company;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.sx.manage.service.helps.IMerchantCompanyInfoService;
import com.sx.manage.servlet.ViewAndModel;
import com.sx.model.constants.MDataStatusType;
import com.sx.model.manage.mybatis.form.consumer.MCompanyForm;
import com.sx.model.manage.mybatis.form.helps.MerchantCompanyInfoForm;
import com.sx.model.manage.mybatis.vo.company.MCompanyVO;
import com.sx.model.manage.mybatis.vo.consumer.login.MConsumerLoginVO;
import com.sx.model.manage.mybatis.vo.helps.MerchantCompanyInfoVO;
import com.sx.model.manage.mybatis.vo.result.ResultBooleanVO;
import com.sx.model.manage.mybatis.vo.result.ResultListVO;
import com.sx.model.manage.mybatis.vo.result.ResultObjectVO;

/**
 * 商户客户关系
 * @author Administrator
 *
 */
@Service("merchantCompanyWebServiceImpl")
@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED)
public class MerchantCompanyWebServiceImpl {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	//商户关系
	@Resource(name="merchantCompanyInfoServiceImpl")
	private IMerchantCompanyInfoService merchantCompanyInfoServiceImpl;
	
	//公司信息
	@Resource(name="mBaseCompanyServiceImpl")
	private IMBaseCompanyService mBaseCompanyServiceImpl;
	
	/** 跳转关系添加界面**/
	public void add_viewCompany(ViewAndModel viewAndModel,Long merchant_id){
		logger.info("跳转商户关系配置界面");
		MCompanyForm form = new MCompanyForm();
		             form.setId(merchant_id);
		//查询商户信息
		ResultObjectVO<MCompanyVO>  resultObjectVO = mBaseCompanyServiceImpl.search_by_bank_Ids(form);
		viewAndModel.addObject("companyVO", resultObjectVO.getObjVo());
		//查询商户关系
		MerchantCompanyInfoForm merForm = new MerchantCompanyInfoForm();
		                        merForm.setMerchant_id(merchant_id);
		                        
		ResultListVO<MerchantCompanyInfoVO> merchantVOList = merchantCompanyInfoServiceImpl.search_all_merchant_company(merForm);
		StringBuilder idStr = new StringBuilder();
		StringBuilder nameStr = new StringBuilder();
		for(MerchantCompanyInfoVO merchantVO : merchantVOList.getData()){
			if( idStr.length()>0){
				idStr.append(",").append(merchantVO.getCompany_id());
				nameStr.append(",").append(merchantVO.getCompany_name());
			}else{
				idStr.append(merchantVO.getCompany_id());
				nameStr.append(merchantVO.getCompany_name());
			}
		}
		viewAndModel.addObject("merchantVOList", merchantVOList.getData());
		viewAndModel.addObject("idStr", idStr.toString());
		viewAndModel.addObject("nameStr", nameStr.toString());
	}
	
	/***增加***/
	public ResultBooleanVO add_merchant_company(MerchantCompanyInfoForm form,MConsumerLoginVO loginConsumer){
		return saveRelation(form,loginConsumer);
	}
	
	/** 增加商户关系 **/
	public ResultBooleanVO saveRelation(MerchantCompanyInfoForm  form, MConsumerLoginVO  loginConsumer ){
		logger.debug("开始增加商户关");
		ResultBooleanVO result = null;
		if(null !=form.getMerchant_id() ){
			logger.debug("根据service_id删除原有的关系");
			result = this.merchantCompanyInfoServiceImpl.del_merchant_company(form, loginConsumer);
			if( !result.isSuccess()){
				logger.debug("根据service_id删除原有的关系时出错"+result.getMsg());
				return result;
			}
		}
		Long[] companyIds = form.getCompany_ids();
		if( null == companyIds || 0 == companyIds.length){
			return new ResultBooleanVO();
		}
		
		List<MerchantCompanyInfoForm> formList = new ArrayList<>(companyIds.length);
		logger.debug("生产发布对象数据集合");
		MerchantCompanyInfoForm merchantForm = null;
		for(int i=0,len = companyIds.length;i<len;i++){
			if( null != companyIds[i]){
				merchantForm = new MerchantCompanyInfoForm();
				
				merchantForm.setCompany_id( companyIds[i] );
				merchantForm.setCompany_name(form.getCompany_names()[i]);
				
				merchantForm.setMerchant_id(form.getMerchant_id());
				merchantForm.setMerchant_name(form.getMerchant_name());
				merchantForm.setCreate_time( new Date());
				merchantForm.setData_status(MDataStatusType.DATA_AVAILABLE.toString());
				
					formList.add( merchantForm );
			}
		}
		logger.debug("开始批量新增商品发布对象数据");
		return this.merchantCompanyInfoServiceImpl.addBatch_merchant_company(formList,loginConsumer);
	}
	
	
	/***删除对象***/
	public ResultBooleanVO del_merchant_company(MerchantCompanyInfoForm form,MConsumerLoginVO loginConsumer){
		merchantCompanyInfoServiceImpl.del_merchant_company(form, loginConsumer);
		return null;
	}
	/***根据对象获取数据***/
	public ResultListVO<MerchantCompanyInfoVO> search_all_merchant_company(MerchantCompanyInfoForm form){
		merchantCompanyInfoServiceImpl.search_all_merchant_company(form);
		return null;
	}
}
